<?php
class ControllerProductSearch extends Controller {
 public function index() {
	$this->language->load('product/search');
	
	//Modifi by Hai Dang
	$this->load->model('catalog/category');
	//End
	
	
	$this->document->title = $this->language->get('heading_title');
	
	$this->document->breadcrumbs = array();
	
	$this->document->breadcrumbs[] = array(
       	'href'      => $this->url->http('common/home'),
       	'text'      => $this->language->get('text_home'),
      	'separator' => FALSE
	);
		
	$url = '';	
	if (isset($this->request->get['keyword'])) {
		$url .= '&keyword=' . $this->request->get['keyword'];
	}
	
	if (isset($this->request->get['description'])) {
		$url .= '&description=' . $this->request->get['description'];
	}
	
	if (isset($this->request->get['sort'])) {
		$url .= '&sort=' . $this->request->get['sort'];
	}
	
	if (isset($this->request->get['order'])) {
		$url .= '&order=' . $this->request->get['order'];
	}
	
	if (isset($this->request->get['page'])) {
		$url .= '&page=' . $this->request->get['page'];
	}
	
	
	
	
	$this->document->breadcrumbs[] = array(
       	'href'      => $this->url->http('product/search' . $url),
       	'text'      => $this->language->get('heading_title'),
      	'separator' => $this->language->get('text_separator')
	);
	
	$this->data['heading_title'] = $this->language->get('heading_title');	
	$this->data['text_critea'] = $this->language->get('text_critea');
	$this->data['text_search'] = $this->language->get('text_search');
	$this->data['text_keywords'] = $this->language->get('text_keywords');
	$this->data['text_empty'] = $this->language->get('text_empty');
	$this->data['text_sort'] = $this->language->get('text_sort');
	
	

	$this->data['entry_description'] = $this->language->get('entry_description');
	$this->data['entry_search'] = $this->language->get('entry_search');
	
	
	$this->data['button_search'] = $this->language->get('button_search');
	
	//Modify by Nguyen Hai Dang
	if (isset($this->request->get['filter_category_id'])) {
			$filter_category_id = $this->request->get['filter_category_id'];
	} else {
		$filter_category_id = 999;
	}
	//$filter_category_id = $this->request->get['filter_category_id'];

	$this->data['text_category'] = $this->language->get('text_category');
	$this->data['filter_category_id'] = $filter_category_id;
	
	//End
	
	//Add by Hai Dang
	// 3 Level Category Search
		$this->data['categories'] = array();
					
		$categories_1 = $this->model_catalog_category->getCategories(0, 1);
		
		foreach ($categories_1 as $category_1) {
			$level_2_data = array();
			
			$categories_2 = $this->model_catalog_category->getCategories($category_1['category_id'], 1);
			
			foreach ($categories_2 as $category_2) {
				$level_2_data[] = array(
					'category_id' => $category_2['category_id'],	
					'name'        => $category_2['name'],
				);					
			}
			
			$this->data['categories'][] = array(
				'category_id' => $category_1['category_id'],
				'name'        => $category_1['name'],
				'children'    => $level_2_data
			);
		}
	//End
	
	
	if (isset($this->request->get['page'])) {
		$page = $this->request->get['page'];
	} else {
		$page = 1;
	}
	
	if (isset($this->request->get['sort'])) {
		$sort = $this->request->get['sort'];
	} else {
		$sort = 'pd.name';
	}
	
	if (isset($this->request->get['order'])) {
		$order = $this->request->get['order'];
	} else {
		$order = 'ASC';
	}
	
	if (isset($this->request->get['keyword'])) {
		$this->data['keyword'] = $this->request->get['keyword'];
	} else {
		$this->data['keyword'] = '';
	}
	
	if (isset($this->request->get['description'])) {
		$this->data['description'] = $this->request->get['description'];
	} else {
		$this->data['description'] = '';
	}

	
	
  	if (isset($this->request->get['keyword'])) {
   	$this->load->model('catalog/product');
   	$product_total = $this->model_catalog_product->getTotalProductsByKeyword_Searching($this->request->get['keyword'],
		isset($this->request->get['description']) ? $this->request->get['description'] : '',
		$filter_category_id);

   	if ($product_total) {
	    $url = '';
	
	    if (isset($this->request->get['description'])) {
	    	$url .= '&description=' . $this->request->get['description'];
	    }
	    
	    $this->load->model('catalog/review');
	    $this->load->model('tool/seo_url');
	    $this->load->helper('image');
	
	    $this->data['products'] = array();
	    $results = $this->model_catalog_product->getProductsByKeyword($this->request->get['keyword'], isset($this->request->get['description']) ? $this->request->get['description'] : '', $sort, $order, ($page - 1) * 12, 12);
		$preProductID = 0;
		$productidArr = array();
		foreach ($results as $result) {
			if(!in_array($result['product_id'], $productidArr)){
			if ($result['image']) {
				$image = $result['image'];
			} else {
				$image = 'no_image.jpg';
			}
	
			$special = $this->model_catalog_product->getProductSpecial($result['product_id']);
	
			if ($special) {
				$special = $this->currency->format($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$special = FALSE;
			}
	
			$rating = $this->model_catalog_review->getAverageRating($result['product_id']);
	                    $date_add = date_create($result['date_added']);
			$this->data['products'][] = array(
	            'name'    => $result['name'],
				'model'   => $result['model'],
				'shop_name'   => $result['shop_name'],
				'href_shop'   => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop&shop=' . $result['shop_name'])),
				'zone'   => $result['zone'],
	            'rating'  => $rating,
			 	'viewed'  => $result['viewed'],
				'date_added'  => date_format ($date_add,"d:m:Y"),
				'stars'   => sprintf($this->language->get('text_stars'), $rating),
				'thumb'   => image_resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
	            'price'   => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
				'special' => $special,
				'href'    => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop_customer&path=' . $result['category_id'] . '&product_id=' . $result['product_id']))
	          );
	          
	          $productidArr[] = $result['product_id'];
	        }
		}
	    if (!$this->config->get('config_customer_price')) {
	     	$this->data['display_price'] = TRUE;
	    } elseif ($this->customer->isLogged()) {
	     	$this->data['display_price'] = TRUE;
	    } else {
	     	$this->data['display_price'] = FALSE;
	    }
	
	    $url = '';
	
	    if (isset($this->request->get['keyword'])) {
	     $url .= '&keyword=' . $this->request->get['keyword'];
	    }
	
	    if (isset($this->request->get['description'])) {
	     $url .= '&description=' . $this->request->get['description'];
	    }
	
	    if (isset($this->request->get['page'])) {
	     $url .= '&page=' . $this->request->get['page'];
	    }
	
	    $this->data['sorts'] = array();
	
	    $this->data['sorts'][] = array(
						'text'  => $this->language->get('text_name_asc'),
						'value' => 'pd.name',
						'href'  => $this->url->http('product/search' . $url . '&sort=pd.name')
	    );
	
	    $this->data['sorts'][] = array(
						'text'  => $this->language->get('text_name_desc'),
						'value' => 'pd.name-DESC',
						'href'  => $this->url->http('product/search' . $url . '&sort=pd.name&order=DESC')
	    );
	
	    $this->data['sorts'][] = array(
						'text'  => $this->language->get('text_price_asc'),
						'value' => 'p.price-ASC',
						'href'  => $this->url->http('product/search' . $url . '&sort=p.price&order=ASC')
	    );
	
	    $this->data['sorts'][] = array(
						'text'  => $this->language->get('text_price_desc'),
						'value' => 'p.price-DESC',
						'href'  => $this->url->http('product/search' . $url . '&sort=p.price&order=DESC')
	    );
	
	    $this->data['sorts'][] = array(
						'text'  => $this->language->get('text_rating_desc'),
						'value' => 'rating-DESC',
						'href'  => $this->url->http('product/search' . $url . '&sort=rating&order=DESC')
	    );
	
	    $this->data['sorts'][] = array(
						'text'  => $this->language->get('text_rating_asc'),
						'value' => 'rating-ASC',
						'href'  => $this->url->http('product/search' . $url . '&sort=rating&order=ASC')
	    );
	
	    $url = '';
	
	    if (isset($this->request->get['keyword'])) {
	     	$url .= '&keyword=' . $this->request->get['keyword'];
	    }
	
	    if (isset($this->request->get['description'])) {
	     	$url .= '&description=' . $this->request->get['description'];
	    }
	
	    if (isset($this->request->get['sort'])) {
	     	$url .= '&sort=' . $this->request->get['sort'];
	    }
	
	    if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
	    }
		
	
	    $pagination = new Pagination();
	    $pagination->total = $product_total;
	    $pagination->page = $page;
	    $pagination->limit = 12;
	    $pagination->text = $this->language->get('text_pagination');
	    $pagination->url = $this->url->http('product/search' . $url . '&page=%s');
	
	    $this->data['pagination'] = $pagination->render();
	
	    $this->data['sort'] = $sort;
	    $this->data['order'] = $order;
   	   }
  	}

	$this->id       = 'content';

	$this->template = $this->config->get('config_template') . 'product/search.phtml';
  	$this->layout   = 'common/layout';
  	$this->render();
  }
}
 ?>
